<?php
namespace app\admin\model;
use think\Model;

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017/11/23
 * Time: 15:20
 */
class LessonComment extends Model
{

    public $comment_rank = [
        1 => '非常满意',
        2 => '满意',
        3 => '基本满意',
        4 => '不太满意',
        5 => '很不满意'
    ];

    // 获取器
    public function getCommentTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }

    public function getCommentRankAttr($value){
        $comment_rank = [
            1 => '非常满意',
            2 => '满意',
            3 => '基本满意',
            4 => '不太满意',
            5 => '很不满意'
        ];
        return $comment_rank[$value];
    }

    /**
     * 查询所有评论信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['bld.lesson_name|u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $data = $this
            ->alias('co')
            ->join('user u', 'co.comment_id=u.id', 'LEFT')
            ->join('lesson bld', 'co.lesson_id=bld.id', 'LEFT')
            ->join('order or', 'co.ord_id=or.id', 'LEFT')
            ->field('co.id,co.comment_id,co.comment_rank,co.comment_content,
                    co.comment_lesson_number,bld.lesson_sn,bld.lesson_name,
                    bld.id as lesson_id,u.full_name,co.comment_time,
                    or.escape_lesson_num')
            ->where($where)
            ->order($order)
            ->select();
        if($data){
            foreach($data as $k=>$v){
                $data[$k] = $v->toArray();
            }
        }

        return $data;
    }

    /**
     * 查询所有评论记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['bld.lesson_name|u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count      = $this
            ->alias('co')
            ->join('user u', 'co.comment_id=u.id', 'LEFT')
            ->join('lesson bld', 'co.lesson_id=bld.id', 'LEFT')
            ->join('order or', 'co.ord_id=or.id', 'LEFT')
            ->field('co.id,co.comment_id,co.comment_rank,co.comment_content,
                    co.comment_lesson_number,bld.lesson_sn,bld.lesson_name,
                    bld.id as lesson_id,u.full_name,co.comment_time,
                    or.escape_lesson_num')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 查询单条记录信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=[],$order=""){
        $info = $this
            ->alias('co')
            ->join('user u', 'co.comment_id=u.id', 'LEFT')
            ->join('lesson bld', 'co.lesson_id=bld.id', 'LEFT')
            ->join('order or', 'co.ord_id=or.id', 'LEFT')
            ->field('co.id,co.comment_id,co.comment_rank,co.comment_content,
                    co.comment_lesson_number,bld.lesson_sn,bld.lesson_name,
                    bld.id as lesson_id,u.full_name,co.comment_time,
                    or.escape_lesson_num')
            ->where($where)
            ->order($order)
            ->find();
        if($info){
            $info = $info ->toArray();
        }
        return $info;
    }
    /*
     * 根据Id获取评论详情
     */
    public function getCommentById($id){
        $info = $this
            ->alias('co')
            ->join('user u', 'co.comment_id=u.id', 'LEFT')
            ->join('lesson bld', 'co.lesson_id=bld.id', 'LEFT')
            ->join('order or', 'co.ord_id=or.id', 'LEFT')
            ->field('co.id,co.comment_id,co.comment_rank,co.comment_content,
                    co.comment_lesson_number,bld.lesson_sn,bld.lesson_name,
                    bld.id as lesson_id,u.full_name,co.comment_time,
                    or.escape_lesson_num')
            ->where("co.id=".$id)
            ->find();
        if($info){
            $info = $info ->toArray();
        }
        return $info;
    }
}